package com.zjz.demo.Pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/09/9:34
 * @Description:
 * @Target:
 */

// 使用线程池了,就要用线程池创建线程
public class ExecutorsTest {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();// 单个线程处理
//         ExecutorService threadPool =Executors.newFixedThreadPool(5);  // 创建一个固定的线程池大小
//        ExecutorService threadPool = Executors.newCachedThreadPool();  // 可伸缩的,遇强则强,遇弱则弱

        try{
            for (int i = 0; i < 100; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ok");
                });
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 线程池的使用完,要关闭
            threadPool.shutdown();
        }

    }
}
